const express = require('express');
const multer = require('multer');
const path = require("path");
const Result = require('../models/Result');
const { UPLOAD_PATH ,UPLOAD_URL } = require('../utils/constant')
const { decoded } = require("../utils");
const  adminArticle  = require('../services/admin_article');
const Boom = require('boom');


const router = express.Router();


router.post(//后台获取文章列表
    '/getAdminArticleList',
    function(req,res){
		let data = req.body
        const decode = decoded(req)
        if(decode && decode.username){
            
            adminArticle.getAdminArticleList(data).then(({list,count,page,limit})=>{
                new Result({
                    list,count,page,limit
                },'请求成功').success(res)
            })
        }
    }
)


router.post('/getArticle',function(req,res,next){//获取文章详情
	let data = req.body
    const decode = decoded(req)
    if(decode && decode.username){
        adminArticle.getArticle(data).then(list=>{
            if(data.type==1){
                new Result(list,'查询成功').success(res)
            }else{
                article.updataArticle({
                    id:data.id,
                    reading_number:list.reading_number+=1
                }).then(data2=>{
                    new Result(list,'查询成功').success(res)
                })	
            }
            
        })
    }
})

router.post('/updataArticle',function(req,res,next){//修改文章
	let data = req.body
    const decode = decoded(req)
    if(decode && decode.username){
        adminArticle.updataArticle(data).then(list=>{
            new Result(list,'修改成功!').success(res)
        })
    }
})

router.post('/addReject',function(req,res,next){//驳回帖子
	let data = req.body
    const decode = decoded(req)
    if(decode && decode.username){
        var data2 = {
            state:data.state,
            id:data.id
        }
        adminArticle.updataArticle(data2).then(list1=>{
            var rejectData={
                aid:data.id,
                msg:data.msg
            }
            adminArticle.addReject(rejectData).then(list2=>{
                new Result(null,'驳回成功!').success(res)
            })
        }) 
    }
})

router.post(//删除文章
    '/removeArticle',
    function(req,res){
        const decode = decoded(req)
        if(decode && decode.username){
            adminArticle.removeArticle(req.body).then(data=>{
                new Result(null,'删除成功').success(res)
            })
        }
    }
)


router.post('/UpdateReject',function(req,res,next){//修改驳回原因
	let data = req.body
    const decode = decoded(req)
    if(decode && decode.username){
        var rejectData={
            aid:data.id,
            msg:data.msg
        }
        adminArticle.updataReject(rejectData).then(list2=>{
            new Result(null,'驳回成功!').success(res)
        })  
    }
})







module.exports = router